import { ref } from 'vue';
import type {Ref} from 'vue';
import {getRecommendList} from '@/api/recommend'
type List={
    appBanHeight: number;
    appBanWidth: number;
    appStatus: string;
    avatar: string;
    buyNow: null;
    categoryIds: string;
    content: null;
    duration: number;
    hasLookCollects: boolean;
    hasVideo: boolean;
    height: number;
    itemIdList: any;
    itemList: null;
    layoutType: number;
    lookList: null;
    newAppBanner: string;
    newAppBannerHeight: number;
    newAppBannerWidth: number;
    nickname: string;
    pcBannerSize: null;
    picUrl: string;
    pubType: number;
    readCount: number;
    schemeUrl: string;
    size: number;
    status: string;
    statusValid: boolean;
    style: number;
    subTitle: string;
    timeStamp: number;
    title: string;
    topType: boolean;
    topicExtend: null;
    topicId: string;
    type: number;
    videoUrl: null;
    wapStatus: string;
    width: number;
}
import { useRouter } from 'vue-router';

export function useRecommendlistHook(){
    const router=useRouter();
    let list:Ref<Array<List>> = ref([]);
    // 决定是否有加载中标记的状态
    let loading = ref(false);
    // 决定是否继续加载的标志
    let finished = ref(false);
    // 刷新中
    let refreshing = ref(false);
    let page=1;
    // 处理加载新数据的函数 也就是发请求
    let onLoad = () => {
        finished.value=false;
        getRecommendList({
            page,
            size: 2,
            exceptIds:""
        })
        .then(res=>{
            refreshing.value = false;
            if (res.data.code==='200') {
                list.value.push(...res.data.data.result[0].topics)
                loading.value=false;
                // 如果没有数据了
                if (res.data.data.result[0].topics.length===0) {
                    // 关闭加载行为
                    finished.value=true;
                }
                // 数据回来列数加一
                page++
            }
        })
    };
    // 当刷新的时候要做什么
    let onRefresh = () => {
      // 清空列表数据
      list.value=[];
      //把开关设置为false
      finished.value = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      loading.value = true;
      onLoad();
    };
    // 点击的时候
    const clickHandler=(schemeUrl:string)=>{
        console.log(schemeUrl);
        
        location.href="https://m.you.163.com"+schemeUrl
    }
    return {refreshing,onRefresh,loading,finished,onLoad,list,clickHandler}
}

